package com.safety.hat.service.impl.admin;

import com.alibaba.fastjson.JSONObject;
import com.safety.hat.data.ACTEnum;
import com.safety.hat.data.Result;
import com.safety.hat.data.admin.AdminGetDeviceDTO;
import com.safety.hat.data.hat.HatHeartInfoBO;
import com.safety.hat.service.admin.IAdminSocketService;
import com.safety.hat.service.hat.adapter.IHatSocketLocationAdapterService;
import com.safety.hat.service.hat.adapter.IHatSocketUserAdapterService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @Author: lijun
 * @Date: 2020/5/25 0:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class AdminSocketDeviceServiceImpl implements IAdminSocketService<String> {

    private final IHatSocketLocationAdapterService hatSocketLocationAdapterService;
    private final IHatSocketUserAdapterService hatSocketUserAdapterService;

    /**
     * 处理消息
     *
     * @param message 消息体
     * @param session 当前Session
     */
    @Override
    public void handleMessage(String message, Session session) {
        ConcurrentHashMap<String, Session> allOnLineHatUser = hatSocketUserAdapterService.getAllOnLineHatUser();
        ConcurrentHashMap<String, List<HatHeartInfoBO>> allLocationInfo = hatSocketLocationAdapterService.getAllInfo();
        List<AdminGetDeviceDTO> deviceDTOList = allOnLineHatUser.entrySet().stream()
                .map(entry -> entry.getKey())
                .map(key -> {
                    List<HatHeartInfoBO> locationInfoList = allLocationInfo.get(key);
                    AdminGetDeviceDTO deviceDTO = AdminGetDeviceDTO.builder()
                            .adviceId(key)
                            .userId(key)
                            .build();
                    if (CollectionUtils.isNotEmpty(locationInfoList)) {
                        HatHeartInfoBO hatHeartInfoBO = locationInfoList.get(locationInfoList.size() - 1);
                        deviceDTO.setXPoint(hatHeartInfoBO.getXPoint());
                        deviceDTO.setYPoint(hatHeartInfoBO.getYPoint());
                    }
                    return deviceDTO;
                })
                .collect(Collectors.toList());
        Result<List<AdminGetDeviceDTO>> result = Result.<List<AdminGetDeviceDTO>>builder()
                .cmd(ACTEnum.ADMIN_GET_ADVICE.getAct())
                .msg("获取成功")
                .data(deviceDTOList)
                .status(true)
                .build();
        session.getAsyncRemote().sendText(JSONObject.toJSONString(result));
    }

    /**
     * 根据当前行为判断是否可以被处理
     */
    @Override
    public boolean canHandle(String act) {
        return ACTEnum.ADMIN_GET_ADVICE.is(act);
    }

}
    

